package com.test.demo.main.adapter

import android.widget.ImageView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.test.demo.R
import com.test.demo.main.bean.ItemTopicDetailsBean
import com.test.demo.utils.glide.widget.SimpleDraweeView

/**
 * 文件名: TopicDetailsChildAdapter
 * 功能描述:
 */
class TopicDetailsChildAdapter :
    BaseMultiItemQuickAdapter<ItemTopicDetailsBean, BaseViewHolder>() {
    init {
        addItemType(ItemTopicDetailsBean.ONE_IMAGE, R.layout.item_feed_one_image)
        addItemType(ItemTopicDetailsBean.TWO_IMAGE, R.layout.item_feed_two_image)
        addItemType(ItemTopicDetailsBean.VIDEO_PLAY, R.layout.item_feed_video_image)
    }

    override fun convert(holder: BaseViewHolder, item: ItemTopicDetailsBean) {
        when (holder.itemViewType) {
            ItemTopicDetailsBean.ONE_IMAGE -> {
                //判断图片是横板还是竖版
                val bigImage = holder.getView<SimpleDraweeView>(R.id.iv_big_image)
//                bigImage.loadUrl()
                bigImage.post {
                    val isHorizontal = getHorizontalvertical(bigImage.width, bigImage.height)
                    if (isHorizontal) {
                        setImageViewDimensions(bigImage, bigImage.width, bigImage.height)
                    } else {
                        //width =w2
                        //h1 = max-height=w2/9*16
                        setVerticalView(bigImage, bigImage.width, bigImage.height)
                    }
                }
            }
            ItemTopicDetailsBean.TWO_IMAGE -> {
                  //二张多张

            }
            ItemTopicDetailsBean.VIDEO_PLAY -> {
                 //视频
            }
        }
    }

    /**
     * 设置竖向大图片
     */
    private fun setVerticalView(bigImage: SimpleDraweeView, width: Int, height: Int) {
        bigImage.layoutParams.width = width
        bigImage.layoutParams.height = height

    }

    /**
     * 判断图片尺寸是横向还是竖向
     */
    private fun getHorizontalvertical(width: Int, height: Int): Boolean {
        return (width > height)
    }

    fun getImageAspectRatio(width: Int, height: Int): Float {
        return width.toFloat() / height.toFloat()
    }

    fun setImageViewDimensions(imageView: ImageView, width: Int, height: Int) {
        // 计算图片的宽高比
        val aspectRatio = getImageAspectRatio(width, height)

        // 判断图片比例，设置宽高
        if (aspectRatio >= (16f / 9f)) {  // 图片更宽或者16:9
            // 比16:9更宽的图片，满宽
            imageView.layoutParams.width = width // 设为宽度
            imageView.layoutParams.height = height // 高度为原始高度
            // 图片裁剪：左右部分
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        } else {  // 比16:9更方的图片
            // 图片更高或者16:9
            imageView.layoutParams.width = width // 定宽
            imageView.layoutParams.height = height // 定高
            // 图片裁剪：上下部分
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        }

        // 更新视图
        imageView.layoutParams = imageView.layoutParams
    }

}